<?php

class Session {
	protected static $options = array(
		'lifetime' => 0,
		'path' => '/',
		'domain' => '',
		'secure' => false,
		'httponly' => true
	);
	
	public static function start() {
		if (self::started()) {
			return true;
		}
		if (!isset($_SESSION)) {
			session_cache_limiter('nocache');
			self::cookie_params();
		}
		return session_start();
	}
	
	public static function started() {
		return (bool)self::id();
	}
	
	public static function read($key) {
		if (array_key_exists($key, $_SESSION)) {
			return $_SESSION[$key];
		}
	}
	
	public static function write($key, $value) {
		$_SESSION[$key] = $value;
	}
	
	public static function delete($key) {
		unset($_SESSION[$key]);
	}
	
	public static function destroy() {
		session_destroy();
	}
	
	public static function id() {
		$id = session_id();
		if (!empty($id)) {
			return $id;
		}
	}
	
	public static function regenerate() {
		self::cookie_params();
		session_regenerate_id();
	}
	
	protected static function cookie_params() {
		session_set_cookie_params(
			self::$options['lifetime'],
			self::$options['path'],
			self::$options['domain'],
			self::$options['secure'],
			self::$options['httponly']
		);
	}
}

?>